package find;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class GoodQuality {

	static String sample = "sample_new.txt";
	static String hquality = "sample_good.txt";
	static String bad = "sample_bad.txt";
	
	static Set<String> badSet;

	public static void main(String[] args) throws IOException {
		readBad();
		int end = 14000000, freq, click;
		String temp, tempa[];
		BufferedReader br = new BufferedReader(new FileReader(sample));
		BufferedWriter bw = new BufferedWriter(new FileWriter(hquality));

		temp = br.readLine();
		for (int i = 0; i < end && temp != null; ++i) {
			tempa = temp.split("\t");
			if (!isBad(temp) && tempa.length == 3) {
				freq = Integer.parseInt(tempa[1]);
				click = Integer.parseInt(tempa[2]);
				if (isGood(freq, click, tempa[0].length())) {
					bw.write(temp);
					bw.newLine();
				}
			}

			if (i % 100000 == 0) {
				System.out.print("Current Process: ");
				System.out.println(i);
			}
			temp = br.readLine();
		}

		br.close();
		bw.close();

	}

	static int FREQ_THRESHOLD = 20;

	static boolean isGood(int freq, int click, int length) {
		if (freq > FREQ_THRESHOLD && click*1.0 / freq > 2 && length >= 50 && length <= 60)
			return true;

		return false;
	}
	
	static boolean isBad(String line) {
		return badSet.contains(line);
	}
	
	static final void readBad() throws IOException {
		badSet = new HashSet<String>();
		BufferedReader br = new BufferedReader(new FileReader(bad));
		String line = br.readLine();
		while (line != null) {
			if (!line.equals(""))
				badSet.add(line);
			line = br.readLine();
		}
		br.close();
	}

}
